/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package JoseH;

import java.sql.*;



public class Personal {
    
    Conectate con;
  
    public Personal (){
    con = new Conectate();
    }
    
    
    public void nuevaPersona(String log, String clave, String nombre){
        try {            
            PreparedStatement pstm = con.getConnection().prepareStatement("insert into " + 
                    "PERSONAL(LOGIN_PERSONAL, CLAVE_PERSONAL, NOMBRE_PERSONAL) " + " values(?,?,?)");            
            pstm.setString(1, log);
            pstm.setString(2, clave);
            pstm.setString(3, nombre);                        
            pstm.execute();
            pstm.close();            
            }
        
        catch(SQLException e){
            System.out.println(e);
            }
        }
    
   
    public void deletePersona(String cod){  
        try {                
            PreparedStatement pstm = con.getConnection().prepareStatement("delete from PERSONAL where LOGIN_PERSONAL = ?");            
                pstm.setString(1, cod);                   
                pstm.execute();
                pstm.close();            
            }
        
        catch(SQLException e){
            System.out.println(e);
            }            
        }
    
     public void updatePersona(String login, String clave, String nombre ){
        try {            
           PreparedStatement pstm = con.getConnection().prepareStatement("update PERSONAL " +
            "set CLAVE_PERSONAL = ? , " +                    
            "NOMBRE_PERSONAL = ? " +      
            "where LOGIN_PERSONAL = ? ");  
            pstm.setString(1, clave);
            pstm.setString(2, nombre);
            pstm.setString(3, login);
            pstm.execute();
            pstm.close();            
      }catch(SQLException e){
            System.out.println(e);
      }
     }

    
 
public Object [][] getDatos(){
    
        int registros = 0;
     
        try {         
            PreparedStatement pstm = con.getConnection().prepareStatement("SELECT count(1) as total FROM PERSONAL ");
            ResultSet res = pstm.executeQuery();
            res.next();
            registros = res.getInt("total");
            res.close();
            }
        
        catch(SQLException e){
            System.out.println(e);
        }
      
    Object[][] data = new String[registros][5];  

        try {    
            PreparedStatement pstm = con.getConnection().prepareStatement("SELECT " +
            " LOGIN_PERSONAL, CLAVE_PERSONAL, NOMBRE_PERSONAL" +
            " FROM PERSONAL" +
            " ORDER BY LOGIN_PERSONAL ");
            ResultSet res = pstm.executeQuery();
        
        int i = 0;
            while(res.next()){
            String estLog = res.getString("LOGIN_PERSONAL");
            String estClave = res.getString("CLAVE_PERSONAL");
            String estNombre = res.getString("NOMBRE_PERSONAL");
            data[i][0] = estLog;            
            data[i][1] = estClave;            
            data[i][2] = estNombre;                    
            i++;
            }
            
        res.close();
        }
        
        catch(SQLException e){
            System.out.println(e);
        }
    
        return data;
        
    }
}